package com.example.wanghao.wangyixinwen.home;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;

import com.example.wanghao.wangyixinwen.R;
import com.example.wanghao.wangyixinwen.act.MainActivity;
import com.example.wanghao.wangyixinwen.bean.NewCenterBean;
import com.example.wanghao.wangyixinwen.fragment.MenuFragment2;
import com.example.wanghao.wangyixinwen.menu.ActionPage;
import com.example.wanghao.wangyixinwen.menu.NewPage;
import com.example.wanghao.wangyixinwen.menu.PicPage;
import com.example.wanghao.wangyixinwen.menu.TopicPage;
import com.example.wanghao.wangyixinwen.utils.HMAPI;
import com.example.wanghao.wangyixinwen.utils.SharedPreferenceTools;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wanghao on 2016/5/18.
 */
public class NewCenterPage extends BasePage {
    @Bind(R.id.news_center_fl)
    FrameLayout mNewsCenterFl;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            newCenterPages.add(new NewPage(mContext,mNewCenterBean.data.get(0)));//新闻界面
            newCenterPages.add(new TopicPage(mContext));//专题界面
            newCenterPages.add(new PicPage(mContext));//组图界面
            newCenterPages.add(new ActionPage(mContext));//活动界面
            MenuFragment2 menuFragment2 = ((MainActivity) mContext).getMenuFragment();
            System.out.println(Thread.currentThread() + "");

            menuFragment2.initMenu(newcenterTitles);

            //设置新闻中心默认界面
           /* mNewsCenterFl.removeAllViews();
            mNewsCenterFl.addView(newCenterPages.get(0).getRootView());
*/

            switchPage(0);
        }
    };
    private int index;//记录新闻中心当前界面的索引值
    private NewCenterBean mNewCenterBean;

    public NewCenterPage(Context context) {
        super(context);
    }

    @Override
    public void onResume() {
        super.onResume();
        //判断当前是哪个界面在展示,就调用哪个界面的onresume方法(注意新闻中心有四个界面)
        if (newCenterPages!=null&&newCenterPages.size()>0){
            newCenterPages.get(index).onResume();
        }

    }

    @Override
    public View initView() {
        /*TextView tv = new TextView(mContext);
        tv.setText("新闻中心");*/

        View view = View.inflate(mContext, R.layout.news_center_frame, null);
        //ViewUtils.inject(this, view);
        ButterKnife.bind(this, view);
        initTitleBar(view);
        return view;
    }



    @Override
    public void initData() {
        //如果本地有缓存数据就先展示
        String result = SharedPreferenceTools.getString(mContext, HMAPI.NEW_CENTER, "");
        if (!TextUtils.isEmpty(result)) {
            parseJson(result);
        }

        //获取网络数据进行展示
        getNetData();
        // System.out.println("新闻中心获取数据");

    }

    //获取网络数据
    private void getNetData() {

        final Request request = new Request.Builder().url(HMAPI.NEW_CENTER).build();
        OkHttpClient okHttpClient = new OkHttpClient();

        //enqueue异步请求
        okHttpClient.newCall(request).enqueue(new Callback() {
            //网络请求失败
            @Override
            public void onFailure(Request request, IOException e) {
                System.out.println("数据响应失败");
            }

            //当网络请求响应的时候
            @Override
            public void onResponse(Response response) throws IOException {
                String result = response.body().string();
                System.out.println(result);
                parseJson(result);
                //获取新闻中心数据后保存到sp中
                SharedPreferenceTools.saveString(mContext, HMAPI.NEW_CENTER, result);
            }
        });


    }

    //新闻中心菜单界面的标题数据集合
    private List<String> newcenterTitles = new ArrayList<>();
    //新闻中心的页面对象集合
    private List<BasePage> newCenterPages = new ArrayList<>();

    private void parseJson(String result) {
        //当成功获取到页面数据后，标记为已经获取数据
        isLoad = true;

        Gson gson = new Gson();
        mNewCenterBean = gson.fromJson(result, NewCenterBean.class);
        System.out.println();

        newcenterTitles.clear();

        //获取左边菜单界面的标题数据,初始化menufragment的列表显示
        for (int i = 0; i < mNewCenterBean.data.size(); i++) {
            newcenterTitles.add(mNewCenterBean.data.get(i).title);
        }


        //传递给左边菜单界面进行展示

        // MenuFragment2 menuFragment2 = new MenuFragment2();





        mHandler.sendEmptyMessage(0);
        //设置新闻中心默认界面
       // mNewsCenterFl.addView(newCenterPages.get(0).getRootView());

     /*   MenuFragment2 menuFragment2 = ((MainActivity) mContext).getMenuFragment();
        System.out.println(Thread.currentThread()+"");
        menuFragment2.initMenu(newcenterTitles);
*/

    }


    //左边菜单界面控制新闻中心进行界面切换
    public void switchPage(int position) {
        index = position;
//        mTxt_title.setText(newcenterTitles.get(position));
        //根据索引值,把对应的界面获取到
        final BasePage basePage = newCenterPages.get(position);
        //先获取到桢布局的内容,再进行添加
        mNewsCenterFl.removeAllViews();
      //  mNewsCenterFl.addView(basePage.getRootView());

        if (!basePage.isLoad){
            basePage.initData();
        }
        //切换桢布局里面的界面
        switch (position) {
            case 0://新闻界面
                mNewsCenterFl.addView(basePage.getRootView());
                break;
            case 1://专题界面
                mNewsCenterFl.addView(basePage.getRootView());
                break;
            case 2://组图界面
                mNewsCenterFl.addView(basePage.getRootView());
                //显示组图按钮
                mBtn_right.setVisibility(View.VISIBLE);
                mBtn_right.setImageResource(R.drawable.icon_pic_grid_type);
                mBtn_right.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //根据组图界面的标记type来控件它的显示方式
                        if (((PicPage)basePage).type){
                            ((PicPage)basePage).type = false;
                            mBtn_right.setImageResource(R.drawable.icon_pic_grid_type);
                            ((PicPage)basePage).swithPageByType();
                        }else{
                            ((PicPage)basePage).type = true;
                            mBtn_right.setImageResource(R.drawable.icon_pic_list_type);
                            ((PicPage)basePage).swithPageByType();
                        }
                    }
                });
                break;
            case 3://活动界面
                mNewsCenterFl.addView(basePage.getRootView());
                break;

        }

    }
}
